<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //设置数据差异信息
    protected $table = 'article';
    protected $primaryKey = 'art_id';

    public $timestamps = false;

    /**
     * 不能被批量赋值的属性(黑名单) 对立的$fillable 白名单
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * 一对一关联模型 关联category 表的 cate_id ('Category', '外键', '本身的字段')
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function ext_cate()
    {
        return $this->hasOne('App\Http\Model\Category', 'cate_id', 'cate_id')->select('cate_id','cate_name');
    }

    public static function cate_article($cate_id)
    {
        //1.先根据$cate_id 查询所有子栏目
        $cate_id_all = Category::where('cate_pid', $cate_id)->get(['cate_id']);

        $field = ['art_id','art_title','art_description','art_thumb','art_time','art_editor','art_view','cate_id','art_order'];
        return self::whereIn('cate_id', [1,6,7,8])->get($field);
    }

    //Article 添加 category 映射
    public function article_category()
    {
        return $this->belongsTo(Category::class, 'cate_id');
    }

}
